package com.scs.application.modules.outCheck.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.outCheck.entity.OutReviewResult;
import com.scs.application.modules.outCheck.entity.SpdWo;
import com.scs.application.modules.outCheck.mapper.SpdWoMapper;
import com.scs.application.modules.outCheck.service.SpdWoService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 */
@Service
public class SpdWoServiceImpl extends BaseServiceImpl<SpdWoMapper, SpdWo> implements SpdWoService {
    @Value("${system.hrp.url}")
    String url;
    @Value("${system.hrp.driver-class-name}")
    String driverClassName;
    @Value("${system.hrp.username}")
    String username;
    @Value("${system.hrp.password}")
    String password;

    public RestResult review(@RequestBody Map<String, Object> para){
        RestResult restResult = RestResult.ok();
        OutReviewResult outReviewResult = new OutReviewResult();
        try {
            List<String> rfidList = (List<String>) para.get("rfids");//实物RFID列表
            String wokey = (String) para.get("wokey");
            List<SpdWo> outObjects = this.list(Wrappers.<SpdWo>query().eq("wo_key",wokey));//出库单明细集合
            List<SpdWo> objects = this.list(Wrappers.<SpdWo>query().eq("wo_key",wokey).in("rfid", rfidList));//实物集合
            List<String> outObjectsRfids = outObjects.stream().map(spdWo -> spdWo.getRfid()).collect(Collectors.toList());
            List<String> objectsRfids = objects.stream().map(spdWo -> spdWo.getRfid()).collect(Collectors.toList());
            List<String> surplusList = (List<String>) CollectionUtils.subtract(rfidList, outObjectsRfids);//实物多
            List<String> lackList = (List<String>) CollectionUtils.subtract(outObjectsRfids, rfidList);//实物少
            List<SpdWo> surplusObjs= new ArrayList();
            surplusList.stream().forEach(rfid -> {
                SpdWo spdWo =new SpdWo();
                spdWo.setRfid(rfid);
                spdWo.setMatrName("未知");
                spdWo.setMatrSpec("未知");
                surplusObjs.add(spdWo);
            });
            outReviewResult.setLackRfids(lackList);
            outReviewResult.setSurplusRfids(surplusObjs);
            outReviewResult.setObjectRfids(objectsRfids);
            outReviewResult.setObjects(objects);
        } catch (Exception e) {
            logger.error("出库复核异常",e);
            restResult.setMsg("出库复核异常");
        }
        restResult.setMsg("出库复核成功");
        restResult.setData(outReviewResult);
        return restResult;
    }

    public PageResult surplusPage() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        IPage pageResult = null;//service.page(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    public List listByWokey(String wokey) {
        if (StringUtils.isBlank(wokey)) return null;
        DriverManagerDataSource dataSource = getDataSource();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql = "SELECT *  FROM VIEW_SPD_WO WHERE wo_key = '" + wokey + "'";
        List<SpdWo> SpdWos = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SpdWo.class));
        return SpdWos;
    }

    private DriverManagerDataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }
}
